package com.fy.controller;

import com.fy.dao.ViedoMapper;
import com.fy.entity.U_co;
import com.fy.entity.Video;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/video")
public class VideoController {
    String id = "1";
    @Resource
    ViedoMapper viedoMapper;


    @GetMapping("/getAll")
    public List<Video> getAll() {
        return viedoMapper.findAll();
    }

    @PostMapping("/getInfo")
    public List<Video> getinfo(String video_id) {
        return viedoMapper.getInfo(video_id);
    }

    @PostMapping("/insert")
    public String insert(String video_id, String video_url,
                         String info_id, String video_title,
                         String video_like, String video_pic,
                         String video_collection, String video_state) {
        viedoMapper.insert(id, video_url,
                info_id, video_title,
                video_like, video_pic,
                video_collection, video_state);
        id = (Integer.valueOf(id) + 1 + "");
        return "200";

    }

    @PostMapping("/update")
    public String update(String video_id, int video_like, int video_collection) {
        List<Video> all = viedoMapper.findAll();
        for (Video v : all) {
            if (video_collection == 0) {
                if (v.getVideo_id().equals(video_id)) {
                    video_like = v.getVideo_like() + 1;
                    video_collection = v.getVideo_collection();
                }

            }
            if (video_like == 0) {
                if (v.getVideo_id().equals(video_id)) {
                    video_collection = v.getVideo_collection() + 1;
                    video_like = v.getVideo_like();
                }

            }
            if (video_collection == -1) {
                if (v.getVideo_id().equals(video_id)) {
                    video_like = v.getVideo_like() - 1;
                    video_collection = v.getVideo_collection();
                }

            }
            if (video_like == -1) {
                if (v.getVideo_id().equals(video_id)) {
                    video_collection = v.getVideo_collection() - 1;
                    video_like = v.getVideo_like();
                }

            }

        }
        viedoMapper.update(video_id, video_like, video_collection);
        return "200";
    }
}
